/*
 * FileCopierProgressDialog.java
 *
 * Created on 15 April 2007, 12:30
 */

package org.cardcopier.ui;

import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;
import org.cardcopier.CardCopierModel;
import org.cardcopier.file.FileCopier;
import org.cardcopier.file.FileCopierListener;
import org.cardcopier.file.FileState;
import org.cardcopier.file.FileUtils;

/**
 *
 * @author  Mark
 */
public class FileCopierProgressDialog extends javax.swing.JDialog implements FileCopierListener {
    
    private static final Logger LOGGER = Logger.getLogger(FileCopierProgressDialog.class);
    
    private final FileCopier fileCopier;
    private final List<FileState> files;
    
    public FileCopierProgressDialog(final java.awt.Frame parent, List<FileState> files, boolean shouldDeleteSourceFileAfterCopy) {
        super(parent, true);
        this.fileCopier = new FileCopier(new FileUtils(), shouldDeleteSourceFileAfterCopy);
        this.files = files;
        initComponents();
    }

    public void startCopy() {
        fileCopier.addFileCopierListener(this);
        Runnable fileCopierRunner = new Runnable() {
            public void run() {
                try {
                    fileCopier.copyFiles(files);
                } catch (IOException e) {
                    String error = "Copy failed because " + e.getMessage();
                    LOGGER.error(error, e);
                    JOptionPane.showMessageDialog(getParent(), error, "Copy Failed!", JOptionPane.ERROR_MESSAGE);
                } finally {
                    copyFinished();
                }
            }
        };
        new Thread(fileCopierRunner).start();
    }
    
    private void copyFinished() {
        setVisible(false);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        copyProgressBar = new javax.swing.JProgressBar();
        currentFile = new javax.swing.JLabel();
        copyProgressLabel = new javax.swing.JLabel();
        cancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("File Copying Progress"));
        copyProgressBar.setMaximum(files.size());
        copyProgressBar.setStringPainted(true);

        currentFile.setText("Current File");

        copyProgressLabel.setText("Copy Progress");

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                        .add(currentFile, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE)
                        .addContainerGap())
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(copyProgressLabel)
                        .addContainerGap(284, Short.MAX_VALUE))
                    .add(copyProgressBar, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(currentFile)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(copyProgressLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(copyProgressBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        cancel.setText("Cancel");
        cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, cancel))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(cancel)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
        copyProgressLabel.setText("Cancelling file copy...");
        fileCopier.cancelCopy();
    }//GEN-LAST:event_cancelActionPerformed

    public void fileCopyStarted(final File sourceFile, final File destinationFile) {
       java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                currentFile.setText(sourceFile.getName());
            }
        });
    }

    public void fileCopyFinished(final File sourceFile, final File destinationFile) {
       java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                int fileNum = copyProgressBar.getValue() + 1;
                currentFile.setText("");
                copyProgressLabel.setText("Copied file " + fileNum + " of " + files.size());
                copyProgressBar.setValue(fileNum);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancel;
    private javax.swing.JProgressBar copyProgressBar;
    private javax.swing.JLabel copyProgressLabel;
    private javax.swing.JLabel currentFile;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
    
}
